<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class Member extends Authenticatable implements MustVerifyEmailContract, JWTSubject
{
    use HasDateTimeFormatter;

    protected $fillable = ['nickname', 'sex', 'avatar', 'open_id', 'sign', 'integral', 'phone', 'mini_type', 'invite_id', 'is_vip', 'vip_end_time', 'vip_show_adv'];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    public function topic()
    {
        return $this->hasMany(Topic::class, 'member_id', 'id')->where(['status' => 1]);
    }

    public function publisher()
    {
        return $this->hasOne(Publisher::class, 'open_id', 'open_id');
    }

    public function favoriteArticles()
    {
        return $this->belongsToMany(Article::class, 'favorites', 'member_id', 'article_id')
            ->withPivot('article_comment_id', 'topic_id', 'topic_comment_id')
            ->withTimestamps();
    }

    public function favoriteArticleComments()
    {
        return $this->belongsToMany(ArticleComment::class, 'favorites', 'member_id', 'article_comment_id')
            ->withPivot('article_id', 'topic_id', 'topic_comment_id')
            ->withTimestamps();
    }

    public function favoriteTopics()
    {
        return $this->belongsToMany(Topic::class, 'favorites', 'member_id', 'topic_id')
            ->withPivot('article_comment_id', 'article_id', 'topic_comment_id')
            ->withTimestamps();
    }

    public function favoriteTopicComments()
    {
        return $this->belongsToMany(TopicComment::class, 'favorites', 'member_id', 'topic_comment_id')
            ->withPivot('article_id', 'topic_id', 'article_comment_id')
            ->withTimestamps();
    }

    public function ArticleComments()
    {
        return $this->hasMany(ArticleComment::class, 'member_id', 'id');
    }

    public function TopicComments()
    {
        return $this->hasMany(TopicComment::class, 'member_id', 'id');
    }

    public function messages()
    {
        return $this->hasMany(Message::class, 'member_id', 'id')->where(['status' => 0]);
    }

    public function signLog()
    {
        return $this->hasMany(MemberSignLog::class, 'member_id', 'id');
    }

    public function taskLog()
    {
        return $this->hasMany(MemberTaskLog::class, 'member_id', 'id');
    }

    public function mini_program()
    {
        return $this->belongsTo(MiniProgram::class, 'mini_type', 'slug');
    }
}
